<?php


class IndexController extends Zend_Controller_Action
{
    protected $mongo;
    protected $db;

    public function init()
    {
        $this->mongo = new Mongo();

        $this->db = $this->mongo->mugshots;

        $this->client = $_SERVER['HTTP_USER_AGENT'];
        
        // test for cookie
        if(isset($_COOKIE['last']))
        	$this->_setParam('i', $_COOKIE['last']);
    }

	/**
	 * Handles both view & voting
	 * 
	 * @param int index
	 * @param int optional v (via POST)
	 *
	 */ 
    public function indexAction()
    {
		// if voting...
		if($this->_hasParam('score'))
		{
			$this->db->shots->update(
				array('seq' => (int)$this->_getParam('i')), 
				array('$inc' => array(
					'votes' => 1, 
					'score' => (int)$this->_getParam('score'),
					"a.stupidity" => (int)$this->_getParam('stupidity', 0), 
					'a.repeat' => (int)$this->_getParam("repeat", 0)
				))
			);
			// if comments... * this can be combined, above, but nulls get in
			if($this->_hasParam('comments') && $this->_getParam('comments') != '')
			{
				$this->db->shots->update(
					array('seq' => (int)$this->_getParam('i')), 
					array('$push' => array(
						'comments' => substr((string)$this->_getParam('comments'), 0, 140)
					))
				);
			}
		} 


		// gender filter
		$this->view->gender = $this->_getParam('gender', 'BOTH');
	
		// get record of type MALE|FEMALE seq >= 
		if($this->view->gender == 'BOTH')
		{
         	$record = $this->db->shots->findOne(
         		array('seq' => array('$gt' => (int)$this->_getParam('i', 0)))
         	);
         	
		} else {
         	$record = $this->db->shots->findOne(
         		array('seq' => array('$gt' => (int)$this->_getParam('i', 0)), 'sex' => (string)$this->_getParam('gender'))
         	);
		}
		
        $this->view->record = $record;
        
        // set cookie
        setcookie('last', $record['seq'], strtotime("+1 year"), "/");
        
		// meh
		if(stristr($this->client, 'mobile'))
			$this->renderScript('index/mobile.phtml');

    }
    
    public function imageAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        
        $id = $this->_getParam('im');
        
        $record = 
        	$this->db->shots
        	->findOne(array("_id" => new MongoId($id)
       	));
        
        $this->getResponse()->clearAllHeaders();
        $this->getResponse()->setHeader("Content-type", "image/jpg");
        $this->getResponse()->setBody($record['image']->bin);
    }
    
    public function thumbAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        
        $id = $this->_getParam('im');
        
        $record = 
        	$this->db->shots
        	->findOne(array("_id" => new MongoId($id)
       	));
        
        // magick
        $im = new Imagick();
        $im->readImageBlob($record['image']->bin);
        
        $size = ((int)$this->_getParam('h') >= 500) ? 500 : (int)$this->_getParam('h');
        
        $im->thumbnailImage($size, null);
        
        $this->getResponse()->clearAllHeaders();
        $this->getResponse()->setHeader("Content-type", "image/jpg");
        $this->getResponse()->setBody($im->getImageBlob());
    }

	public function searchAction()
	{
		$query = $this->_getParam('sq');

		$records = $this->db->shots->find(array(
			"name" => new MongoRegex("/$query/i")), 
			array(
				"seq" => 1, 
				"name" => 1, 
				"score" => 1,
				"arrest_date" => 1,
				"offenses" => 1
			))->timeOut(90000);

		$this->view->records = $records;

	}

}

